package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 15:47
 * To change this template use File | Settings | File Templates.
 */
public class ConstructorCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;
    private String suffix;

    public ConstructorCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    public ConstructorCodeGenerator(WsdtEntity wsdtEntity, String suffix) {
        this.wsdtEntity = wsdtEntity;
        this.suffix = suffix;
    }

    @Override
    public String codeGenerate() {
        String code="";
        String fieldsCode="";
        String getterSetterCode="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    fieldsCode+=""+wsdtField.getFieldType()+" "+wsdtField.getFieldName()+",";
                    getterSetterCode+="        this."+wsdtField.getFieldName()+" = "+wsdtField.getFieldName()+";\n";
                }
            }
        }
        if(!"".equals(fieldsCode)){
            fieldsCode=fieldsCode.substring(0,fieldsCode.length()-1);
            if(suffix!=null){
                code += "public " + wsdtEntity.getEntityName() + suffix + "(" + fieldsCode + ") {\n";
            }else {
                code += "public " + wsdtEntity.getEntityName() + "(" + fieldsCode + ") {\n";
            }
            code+=getterSetterCode;
            code+="    }\n";
        }
        return code;
    }
}
